{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 一、ndarray\n",
    "## 1.ndarray的特性\n",
    "### 多维性"
   ],
   "id": "5b7d53c26147c4ad"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "e4b1781a7781aab0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array(5)  # 创建0维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "de5085de949612a0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2, 3])  # 创建1维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "9d036ab5cd7b216b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([ [1, 2, 3], [4, 5, 6]])  # 创建2维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "be1480f704e0bfd9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 同质性",
   "id": "af8877098288bdb8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 'hello'])  # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "ade3d72a4caa8e8b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2.5])  # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "8b657b116795f286",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.ndarray的属性",
   "id": "82b7115d7db0c81f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array(1)\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "ba35da240b2be6ac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2.5, 3])\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "cd82f5595a5b9561",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "3a82fc54fb013ec5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.ndarray的创建",
   "id": "bf9f1badabeac91f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 基础的创建方法\n",
    "list1 = [4, 5, 6]\n",
    "arr = np.array(list1,dtype=np.float64)\n",
    "print(arr.ndim)  # 属性\n",
    "print(arr)"
   ],
   "id": "f535620cd2b10fd3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# copy\n",
    "arr1 = np.copy(arr)  # 元素跟原始的数组相同，但是不是一个数组了\n",
    "print(arr1)\n",
    "arr1[0] = 8\n",
    "print(arr1)\n",
    "print(arr)"
   ],
   "id": "f524e13d77cb16a1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 预定义形状\n",
    "# 全0  全1  未初始化  固定值\n",
    "# 全0\n",
    "arr = np.zeros((2,3),dtype=int)\n",
    "print(arr)\n",
    "print(arr.dtype)"
   ],
   "id": "ad664389195b94f0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.zeros((200,),dtype=int)\n",
    "print(arr)"
   ],
   "id": "62e4c0fb5f67187c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 全1\n",
    "arr = np.ones((5,8),dtype=int)\n",
    "print(arr)"
   ],
   "id": "a15738def6cc08bc",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 未初始化\n",
    "arr = np.empty((2,3))\n",
    "print(arr)"
   ],
   "id": "9c84a30f3b02e86b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.empty((4, 2))\n",
    "print(arr)"
   ],
   "id": "4f8fe0ed498d8a1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.full((3,4),2025)\n",
    "print(arr)"
   ],
   "id": "53004b9b1dfad1d2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr1 = np.zeros_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.empty_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.ones_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.full_like(arr,2026)\n",
    "print(arr1)"
   ],
   "id": "587e7bbe0d00ccef",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 等差数列 2 4 6 8 10\n",
    "arr = np.arange(1, 51, 1) # start,end,step(步长）\n",
    "print(arr)"
   ],
   "id": "daf665a7206c17d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 等间隔数列\n",
    "arr = np.linspace(0,100,5)\n",
    "print(arr)"
   ],
   "id": "389eb32ecaff854b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.linspace(0,100,5,dtype=int)\n",
    "print(arr)\n",
    "arr = np.arange(0,101, 25)\n",
    "print(arr)"
   ],
   "id": "41cef360863c848b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 对数间隔数列\n",
    "arr = np.logspace(0,4,3,base=2)\n",
    "print(arr)"
   ],
   "id": "90b51f297aea142e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.linspace(0,4,3)\n",
    "print(arr)"
   ],
   "id": "8ee2cb45493c7708",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.logspace(0,4,3)\n",
    "print(arr)"
   ],
   "id": "d46338e1d98138d4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 特殊矩阵\n",
    "# 单位矩阵：主对角线上的数字为1，其他的数字为0\n",
    "arr = np.eye(3,4,dtype=int)\n",
    "print(arr)\n"
   ],
   "id": "cd4d4a241313ddb3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 对角矩阵：主对角线上非零的0，其他的数字为0\n",
    "arr = np.diag([5,1,2,3])\n",
    "print(arr)"
   ],
   "id": "70ca6207cf4d6615",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 随机数组的生成\n",
    "# 生成0到1之间的随机浮点数（均匀分布）\n",
    "arr = np.random.rand(2,3)\n",
    "print(arr)"
   ],
   "id": "5899d8396e0512ac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机浮点数\n",
    "arr = np.random.uniform(3, 6,(2,3))\n",
    "print(arr)"
   ],
   "id": "37eb3663103b28b7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机整数\n",
    "arr = np.random.randint(3, 30,(2,3))\n",
    "print(arr)"
   ],
   "id": "11e278f1ea7324e4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 生成随机数列（正态分布）（-3到3之间）\n",
    "# 两边小，中间大\n",
    "arr = np.random.randn(2,3)\n",
    "print(arr)"
   ],
   "id": "c0e7d66f9746ab01",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 设置随机种子\n",
    "np.random.seed(20)\n",
    "arr = np.random.randint(1,10,(2,5))\n",
    "print(arr)"
   ],
   "id": "582c684ffc18fded",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2,3],dtype='i8')\n",
    "print(arr)"
   ],
   "id": "192bbdd6ae4a7507",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "4. ndarray的数据类型\n",
    "\n",
    "布尔类型 bool\n",
    "\n",
    "整数类型  int  uint\n",
    "\n",
    "浮点数   float\n",
    "\n",
    "复数  complex"
   ],
   "id": "c4bb10d7beb81c53"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1,0,127,0],dtype= np.int8)\n",
    "print(arr)"
   ],
   "id": "42cc332210f3c851",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "5. 索引与切片",
   "id": "18837d493aec96ab"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 一维数组的索引与切片\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)"
   ],
   "id": "7ad0edd8f6d3c7ba",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "print(arr[10])\n",
    "print(arr[:]) # 获取全部的数据\n",
    "print(arr[2:5])  # start:end+1 左包右不包\n",
    "print(arr[slice(2,15,3)])  #start,end,step\n",
    "print(arr[ (arr>10)  & (arr<70) ]) # 布尔索引"
   ],
   "id": "af99e4f18af4d41a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 二维数组的索引与切片\n",
    "arr = np.random.randint(1,100,(4,8))\n",
    "print(arr)"
   ],
   "id": "39146d581203eded",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "print(arr[1,3])  # 索引\n",
    "print(arr[ 1,2:5])\n",
    "print(arr[2][  arr[2] > 50 ])\n",
    "print( arr[:,3] )"
   ],
   "id": "f9fb53a5e4393367",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "5. ndarray的运算",
   "id": "96372ce24e1d321d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 算术运算\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a ** 2)"
   ],
   "id": "2be827ababd2002a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "c = [1, 2, 3]\n",
    "d = [4, 5, 6]\n",
    "for i in range(len(c)):\n",
    "    d[i] = d[i]+c[i]\n",
    "print(d)"
   ],
   "id": "34671bea306216d2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "b = np.array([[4,5,6],[7,8,9],[1,2,3]])\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a ** 2)"
   ],
   "id": "717916a1684b1e3f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 数组与标量之间的算术运算\n",
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(a + 3)\n",
    "print(a * 3)"
   ],
   "id": "f282910a8bf3855c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 广播机制: 1.获取形状 2.是否可广播\n",
    "# 同一维度：相同、1\n",
    "a = np.array([1,2,3]) #1*3\n",
    "b = np.array([[4],[5],[6]])  #3*1\n",
    "print(b - a )\n",
    "'''a\n",
    " 1 2 3\n",
    " 1 2 3\n",
    " 1 2 3\n",
    " b\n",
    " 4 4 4\n",
    " 5 5 5\n",
    " 6 6 6\n",
    "'''"
   ],
   "id": "f94e5d0ead6413be",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "a = np.array([1,2,3]) #1*3\n",
    "b = np.array([4,5])  #1*2\n",
    "print(b - a )"
   ],
   "id": "e1fd3560ea778ac5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 矩阵运算\n",
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "b = np.array([[4,5,6],[7,8,9],[1,2,3]])\n",
    "print(a @ b)"
   ],
   "id": "90cbefa6160f8ebf",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "'''a\n",
    "1 2 3\n",
    "4 5 6\n",
    "7 8 9\n",
    "b\n",
    "4 5 6\n",
    "7 8 9\n",
    "\n",
    "1 2 3'''\n"
   ],
   "id": "e8a83ac605803b40",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 2.3 numpy中的常用函数\n",
    "## 1. 基本数学函数"
   ],
   "id": "1e688453f961ff05"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算平方根\n",
    "print(np.sqrt(9))\n",
    "print(np.sqrt([1,4,9]))\n",
    "arr = np.array([1,25,81])\n",
    "print(np.sqrt(arr))"
   ],
   "id": "d9f9f3e02aad59d9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算指数  e^x  = y\n",
    "print(np.exp(1))"
   ],
   "id": "e951d6fdbe07350",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算自然对数  ln y = x\n",
    "print(np.log(2.71))"
   ],
   "id": "afdfc019973f74ed",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算正弦值、余弦值\n",
    "print(np.sin(np.pi/2))\n",
    "print(np.cos(np.pi))"
   ],
   "id": "ce24dd0da699e37f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算绝对值\n",
    "arr = np.array([-1, 1, 2, -3])\n",
    "print(np.abs(arr))"
   ],
   "id": "288f4b64e1f43aca",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算a的b次幂\n",
    "print(np.power(arr,3))"
   ],
   "id": "29d329f577c87552",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 四舍五入\n",
    "print(np.round([3.2, 4.5, 8.1, 9.6]))"
   ],
   "id": "909566c361910b44",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 向上取整，向下取整\n",
    "arr = np.array([1.6, 25.1, 81.7])\n",
    "print(np.ceil(arr))\n",
    "print(np.floor(arr))"
   ],
   "id": "eb21b7517fa25c4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 检测缺失值NaN\n",
    "np.isnan([1, 2, np.nan, 3])"
   ],
   "id": "8d56a71675fb5d06",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "2.统计函数\n",
    "\n",
    "求和、计算平均值、计算中位数、标准差、方差\n",
    "\n",
    "查找最大值、最小值\n",
    "\n",
    "计算分位数、累积和、累积差\n"
   ],
   "id": "273e9f47bd2f3da1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,200,8)\n",
    "print(arr)"
   ],
   "id": "8540912daa19baf8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 求和\n",
    "print(np.sum([1,2,3]))"
   ],
   "id": "68a5070edf6420e8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算平均值\n",
    "print(np.mean(arr))"
   ],
   "id": "39548c9c05410d0d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算中位数\n",
    "# 奇数：排序后中间的数值\n",
    "# 偶数：中间的两个数的平均值\n",
    "print(np.median([4,1,2]))\n",
    "print(np.median([1,1,1,1,1,1,1,1,1,100]))"
   ],
   "id": "7d805f8a041bea9f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算标准差、方差\n",
    "# 1,2,3 的平均值 2\n",
    "# ((1-2)^2 + (2-2)^2 + (3-2)^2 )/ 3 = 0.666\n",
    "arr1 = np.array([1,2,1,2,1,1,1,2])\n",
    "arr2 = np.array([1,0,3,0,0,0,4,3])\n",
    "print(np.mean(arr1))\n",
    "print(np.mean(arr2))\n",
    "print(np.var(arr1))\n",
    "print(np.var(arr2))"
   ],
   "id": "2f58c0a6c6276fc1",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 计算最大值、最小值\n",
    "print(arr)\n",
    "print(np.max(arr), np.argmax(arr))\n",
    "print(np.min(arr), np.argmin(arr))"
   ],
   "id": "3a5ea7e65d585934",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 分位数\n",
    "# 中位数\n",
    "# __________________\n",
    "# 1     2    3     4\n",
    "# 1       2        3\n",
    "print(np.median([1,2,3]))\n",
    "print(np.median([1,2,3,4]))\n",
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,100,4)\n",
    "print(arr)\n",
    "# __________________\n",
    "# 44   47   64   67\n",
    "# (47+64)/2\n",
    "print(np.median(arr))\n",
    "print(np.percentile(arr,80))\n",
    "# 0.25*3=0.75\n",
    "# (47-44)*0.75=2.25 + 44 = 46.25\n",
    "# 3*0.8 = 2.4\n",
    "# (67-64)*0.4 = 1.2 + 64 = 65.2"
   ],
   "id": "3b6a8b744bb58cd8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 累积和、累积积\n",
    "arr = np.array([1,2,3])\n",
    "print(np.sum(arr))\n",
    "print(np.cumsum(arr))\n",
    "print(np.cumprod(arr))"
   ],
   "id": "9e74f3683401778a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 创建嵌套JSON结构的数据\n",
    "import numpy as np\n",
    "products = {\n",
    "    \"products\": [\n",
    "        {\n",
    "            \"id\": f\"P{i}\",\n",
    "            \"name\": f\"Product {i}\",\n",
    "            \"category\": np.random.choice([\"Electronics\", \"Clothing\", \"Home\", \"Food\"]),\n",
    "            \"price\": round(np.random.uniform(5, 200), 2),\n",
    "            \"in_stock\": np.random.choice([True, False]),\n",
    "            \"specs\": {\n",
    "                \"weight\": round(np.random.uniform(0.1, 5), 2),\n",
    "                \"dimensions\": {\n",
    "                    \"length\": np.random.randint(5, 50),\n",
    "                    \"width\": np.random.randint(5, 30),\n",
    "                    \"height\": np.random.randint(5, 20)\n",
    "                }\n",
    "            }\n",
    "        } for i in range(1, 21)\n",
    "    ]\n",
    "}\n",
    "\n",
    "# 保存为JSON\n",
    "import json\n",
    "with open('data/products.json', 'w') as f:\n",
    "    json.dump(products, f, indent=2)"
   ],
   "id": "e8ea8fafcc484fde",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
